package com.saibo.spider.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * 字符encode工具类
 * @author zhangzhening
 */
public class EncodeUtil {

	public static final String encodeURL(String str, String enc) {
		try {
			return URLEncoder.encode(str, enc);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}

	public static final String decodeURL(String str, String enc) {
		try {
			return URLDecoder.decode(str, enc);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}

	public static void main(String[] args) {
		String stest = "https://www.baidu.com/s?wd=http%E8%AF%B7%E6%B1%82%E5%B7%A5%E5%85%B7%E7%B1%BB&pn=10&oq=http%E8%AF%B7%E6%B1%82%E5%B7%A5%E5%85%B7%E7%B1%BB&tn=monline_5_dg&ie=utf-8&rsv_pq=ef9693bd00002d31&rsv_t=413aF9WjHzzvDE56EH%2FBj%2FSB7CYzLsEcy%2F2J2eHj2uIqt6SBYmMteF7sQ68cnnf183Z4&rsv_page=1";

		String a = EncodeUtil.decodeURL(stest, "utf-8");
		System.out.println(a);
	}

}
